using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using miciv;
using micivCore.Data;
using micivCore.Data.UnitTechniques;

namespace micivCore.Components
{
    public class CoUnitManager : ComponentManager<CoUnitProto, CoUnit, CoUnitManager>
    {
        protected Hashtable m_htblTechniqueTypes;

        public CoUnitManager(CoreContext _coreContext)
            : base(_coreContext)
        {
            this.m_vstrAvailableComponentNames.Add("unit");

            this.CoreContext.AddData("UnitManager", new UnitManager(this.CoreContext));

            // register all technique types
            this.m_htblTechniqueTypes = new Hashtable();
            this.m_htblTechniqueTypes.Add("move", typeof(MoveTechnique));
            this.m_htblTechniqueTypes.Add("attack", typeof(AttackTechnique));
            this.m_htblTechniqueTypes.Add("create", typeof(CreateTechnique));
        }

        public bool AddTechniqueType(string _strXmlElementName, Type _type)
        {
            if ((false == this.m_htblTechniqueTypes.ContainsKey(_strXmlElementName)) && (null != _type.GetInterface("IUnitTechnique")))
            {
                this.m_htblTechniqueTypes.Add(_strXmlElementName, _type);
                return true;
            }
            return false;
        }

        internal IUnitTechnique CreateTechnique(XmlElement _xmlTechnique)
        {
            if (false != this.m_htblTechniqueTypes.ContainsKey(_xmlTechnique.Name))
            {
                Type techniqueType = this.m_htblTechniqueTypes[_xmlTechnique.Name] as Type;
                object[] parameters = { this.m_coreContext, _xmlTechnique };
                try
                {
                    return Activator.CreateInstance(techniqueType, parameters) as IUnitTechnique;
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                }
            }
            return null;
        }
    }
}
